﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace GBH.Models.Dao
{
    public class MaterialLocationDao
    {
        public List<MaterialLocation> GetMatLocation(string MatNo)
        {
            using (var db = new GBHEntities())
            {
                var matLocation = (from ml in db.MaterialLocations
                                   where ml.MatNo == MatNo
                                   select ml).ToList();
                return matLocation;
            }
        }

        public List<MaterialLocation> GetMatLocationByBranchNO(string BranchNo)
        {
            using (var db = new GBHEntities())
            {
                var matLocation = (from ml in db.MaterialLocations
                                   where ml.LocationNo == BranchNo && ml.IsEnable == true
                                   select ml).ToList();
                return matLocation;
            }
        }

        public bool SaveMatLocations(List<MaterialLocation> MatLocations)
        {
            try
            {
                using (var db = new GBHEntities())
                {
                    int result = db.Database.ExecuteSqlCommand("DELETE FROM MaterialLocation");
                    db.MaterialLocations.AddRange(MatLocations);
                    db.SaveChanges();
                }

                return true;
            }
            catch (Exception)
            {

                return false;
            }
            
        }

        public bool SaveMatLocation(MaterialLocation MatLocation)
        {
            using (var db = new GBHEntities())
            {
                var matLocation = db.MaterialLocations.Where(m => m.MatNo == MatLocation.MatNo & m.LocationNo == MatLocation.LocationNo).FirstOrDefault();
                if (matLocation == null)
                {
                    db.MaterialLocations.Add(MatLocation);
                }
                else
                {
                    matLocation.IsEnable = MatLocation.IsEnable;
                }
                int result = db.SaveChanges();
                return result > 0;
            }
        }

        public bool InsertMatLocation(MaterialLocation MatLocation)
        {
            using (var db = new GBHEntities())
            {
                var matLocation = db.MaterialLocations.Add(MatLocation);
                int result = db.SaveChanges();
                return result > 0;
            }
        }

        public bool InsertMatLocation(List<MaterialLocation> MatLocations)
        {
            if (MatLocations != null && MatLocations.Count > 0)
            {
                using (var db = new GBHEntities())
                {
                    db.Database.ExecuteSqlCommand("delete from MaterialLocation where MatNo = '" + MatLocations[0].MatNo + "'");
                    var matLocation = db.MaterialLocations.AddRange(MatLocations);
                    int result = db.SaveChanges();
                    return result > 0;
                }
            }
            return false;
        }

        public bool UpdateMatLocation(MaterialLocation EditMatLocation)
        {
            using (var db = new GBHEntities())
            {
                var matLocation = db.MaterialLocations.Where(m => m.Id == EditMatLocation.Id).FirstOrDefault();
                if (matLocation != null)
                {
                    matLocation = EditMatLocation;
                    int result = db.SaveChanges();
                    return result > 0;
                }

                return false;
            }
        }

        public bool DeleteMatLocation(int MatLocationId)
        {
            using (var db = new GBHEntities())
            {
                var matLocation = db.MaterialLocations.Where(m => m.Id == MatLocationId).FirstOrDefault();
                if (matLocation != null)
                {
                    db.MaterialLocations.Remove(matLocation);
                    int result = db.SaveChanges();
                    return result > 0;
                }

                return false;
            }
        }

        public bool DeleteMatLocation(MaterialLocation MatLocation)
        {
            using (var db = new GBHEntities())
            {
                if (MatLocation != null)
                {
                    db.MaterialLocations.Remove(MatLocation);
                    int result = db.SaveChanges();
                    return result > 0;
                }

                return false;
            }
        }
    }
}